using System;
using System.Collections.Generic;
using System.Text;

using CMS.Data;
using CMS.Core.Domain;
using CMS.Core.DataInterfaces;
using CMS.Presenters.Views;
using AjaxPro;

namespace CMS.Presenters.Presenters
{
    public class ListDiaBanPresenter
    {
        private List<DiaBan> objList;
        private IListDiaBanView iObjView;
        private IDiaBanDao iObjDAO;
        
        /// <summary>
        /// Raised when the user wants to cancel editing a object.  The containing ASPX page should 
        /// listen for this event.
        /// </summary>
        public EventHandler CancelUpdateEvent;

        /// <summary>
        /// Raised after the object has been successfully committed to the database. 
        /// </summary>
        public EventHandler UpdateCompleteEvent;

        public ListDiaBanPresenter()
        { 
        
        }

        //Attaches this presenter to the view's events.
        public ListDiaBanPresenter(IListDiaBanView iObjView, IDiaBanDao iObjDAO)
        {
            if (iObjView == null) throw new ArgumentNullException("View may not be null");
            if (iObjDAO == null) throw new ArgumentNullException("LinhVucDao may not be null");
            this.iObjView = iObjView;
            this.iObjDAO = iObjDAO;

            SubscribeViewToEvents();
        }  

        void SubscribeViewToEvents()
        {
            iObjView.Load += OnViewLoad;
        }

        void OnViewLoad(object sender, EventArgs e)
        {
             //LoadViewFromModel();
        }

        public void LoadViewFromModel(string maNSD)
        {
            this.objList = this.iObjDAO.GetAllOrderMoreFields(new string[] { "ThuTu" }, true);
            iObjView.DiaBanList = objList;
            // iObjView.IsRole = NHibernateDaoFactory.Instance.GetKiemTraQuyenDao().CheckQuyenCapNhatThongTinDanhMuc(maNSD);
        }

        [AjaxMethod]
        public bool DeleteDiaBan(int maDiaBan)
        {
            DiaBan diaban = new DiaBanDao().GetById(maDiaBan);
            DiaBanDao diaBanDao = new DiaBanDao();
            if (diaBanDao.GetFieldEqualCriteria("MaDiaBanTrucThuocLookup", diaban).Count > 0)
            {
                return false;
            }
            if (new DonThuDao().GetFieldEqualCriteria("DiaBan", diaban).Count > 0)
            {
                return false;
            }

            diaBanDao.Delete(diaban);
            return true;
        }

        #region Button Members
        // For Cancel button
        public void CancelUpdate()
        {
            CancelUpdateEvent(this, null);
        }

        // For Delete button
        public void ObjToDelete(int maDiaBan)
        {
            this.iObjDAO.Delete(iObjDAO.GetById(maDiaBan, false));            
        }

        // For Update button
        public void ObjToUpdate(int maDiaBan)
        {
            DiaBan obj = iObjDAO.GetById(maDiaBan, false);
            obj.TrangThai = !obj.TrangThai;
            this.iObjDAO.SaveOrUpdate(obj);
        }
        #endregion
    }
}
